package com.java.androidmarket.service.impl;

import com.java.androidmarket.dao.GoodsDao;
import com.java.androidmarket.entity.Goods;
import com.java.androidmarket.entity.OrderGoods;
import com.java.androidmarket.service.GoodsService;
import com.java.androidmarket.utils.JWTUtils;
import com.java.androidmarket.utils.RedisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.TimeUnit;

/**
 * description:
 * author:张腾
 * date:2021-04-21
 */

@Service
public class GoodsServiceImpl implements GoodsService {

    @Autowired
    private GoodsDao goodsDao;
    @Autowired
    private RedisUtils redisUtils;

    @Override
    public List<Goods> selectForHomePage() {

        List<Goods> goods = goodsDao.selectForHomePage();
        return goods;
    }

    @Override
    public Goods getGoodsDetails(int goodsId) {

        Goods goods = goodsDao.goodsDetails(goodsId);
        return goods;
    }

    @Override
    public double getTotalPrice(List<OrderGoods> goodsList) {
        double totalPrice = 0;
        for (OrderGoods orderGoods : goodsList) {
            Goods goods = goodsDao.getGoodsPrice(orderGoods.getGoodsId());
            totalPrice += goods.getGoodsPrice()*goods.getDiscount()*orderGoods.getGoodsNum();
        }
        return totalPrice;
    }

    @Override
    public List<Goods> selectByCate(String goodsCategory, int goodsPage) {
        goodsPage = 16*(goodsPage-1);
        List<Goods> goods = goodsDao.selectByCate(goodsCategory, goodsPage);
        return goods;
    }

    @Override
    public List<Goods> search(String search) {

        List<Goods> goods = goodsDao.search(search);
        return goods;
    }

    @Override
    public List<Goods> getKillGoods() {
        return goodsDao.getKillGoods();
    }

    @Override
    public boolean checkKillTime() throws ParseException {
        Date date = new Date();
        Calendar instance = Calendar.getInstance();
        String timeBegin = instance.get(Calendar.YEAR)+"-"+Integer.valueOf(instance.get(Calendar.MONTH)+1)+"-"+instance.get(Calendar.DATE)+" 20:00:00";
        String timeEnd = instance.get(Calendar.YEAR)+"-"+Integer.valueOf(instance.get(Calendar.MONTH)+1)+"-"+instance.get(Calendar.DATE)+" 20:30:00";
//        System.out.println(time);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA);
        Date killTimeBegin = sdf.parse(timeBegin);
        Date killTimeEnd = sdf.parse(timeEnd);
        return date.after(killTimeBegin) && date.before(killTimeEnd);
    }

    @Scheduled(cron = "0 0 20 * * ?")
    public void setKillBegin(){
        List<Goods> killGoodsList = getKillGoods();
        for (Goods goods : killGoodsList) {
            redisUtils.set("KillGoods"+goods.getGoodsId(),"Yes",30L, TimeUnit.MINUTES);
        }
//        System.out.println("hello");
    }


    @Override
    public void dealCountChange(int goodsId, int goodsNum) {
        Goods goods = new Goods();
        goods.setGoodsId(goodsId).setDealCount(goodsNum);
        goodsDao.dealCountChange(goods);
    }


}
